<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-if="formThead.id" v-model="listQuery.id" style="width: 200px;" class="filter-item" placeholder="id" clearable @keyup.enter.native="handleFilter"/>
      <el-input v-if="formThead.user_id" v-model="listQuery.user_id" style="width: 200px;" class="filter-item" placeholder="商家或者用户ID" clearable @keyup.enter.native="handleFilter"/>
      <div v-if="formThead.user_type" class="filter-item">
        <el-select v-model="listQuery.user_type" placeholder="用户类型" clearable @keyup.enter.native="handleFilter">
          <el-option v-for="op in select_user_type" :key="op.value" :label="op.label" :value="op.value"/>
        </el-select>
      </div>
      <el-input v-if="formThead.content" v-model="listQuery.content" style="width: 200px;" class="filter-item" placeholder="意见内容" clearable @keyup.enter.native="handleFilter"/>
      <div v-if="formThead.create_time" class="filter-item">
        <el-date-picker v-model="listQuery.create_time" type="datetime" placeholder="创建时间" clearable @keyup.enter.native="handleFilter"/>
      </div>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">搜索</el-button>
      <addPage @getLists="getLists"/>
    </div>

    <div class="filter-container">
      <el-checkbox v-model="formThead.id" label="id">id</el-checkbox>
      <el-checkbox v-model="formThead.user_id" label="用户ID">用户ID</el-checkbox>
      <el-checkbox v-model="formThead.user_type" label="用户类型">用户类型</el-checkbox>
      <el-checkbox v-model="formThead.content" label="意见内容">意见内容</el-checkbox>
      <el-checkbox v-model="formThead.create_time" label="创建时间">创建时间</el-checkbox>
    </div>

    <el-table v-loading="listLoading" :key="tableKey" :data="list" border fit highlight-current-row style="width: 100%;">
      <el-table-column v-if="formThead.id" label="id" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column v-if="formThead.user_id" label="用户ID" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.user_id }}</span>
          <!--<span>{{ session }}</span>-->
        </template>
      </el-table-column>
      <el-table-column v-if="formThead.user_name" label="名称" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.user_name }}</span>
        </template>
      </el-table-column>
      <!--<el-table-column v-if="formThead.user_type" label="用户类型" align="center">-->
      <!--<template slot-scope="scope">-->
      <!--<span v-if="scope.row.user_type === ''">—</span>-->
      <!--</template>-->
      <!--</el-table-column>-->
      <!--<el-table-column v-if="formThead.user_type" label="用户类型" align="center">-->
      <!--<template slot-scope="scope">-->
      <!--<span>{{ scope.row.user_type }}</span>-->
      <!--</template>-->
      <!--</el-table-column>-->
      <el-table-column v-if="formThead.user_type_name" label="用户类型" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.user_type_name }}</span>
        </template>
      </el-table-column>
      <el-table-column v-if="formThead.content" label="意见内容" align="center">
        <template slot-scope="scope">
          <span><viewContent :row_data="scope.row"/></span>
        </template>
      </el-table-column>
      <el-table-column v-if="formThead.create_time" label="创建时间" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.create_time | formatDate }}</span>
          <!--<span>{{ scope.row.create_time }}</span>-->
        </template>
      </el-table-column>
      <!--<el-table-column align="center" label="操作" class-name="small-padding fixed-width">-->
      <!--<template slot-scope="scope">-->
      <!--&lt;!&ndash;编辑&ndash;&gt;-->
      <!--<editPage :row_data="scope.row" @getLists="getLists"/>-->
      <!--&lt;!&ndash;删除&ndash;&gt;-->
      <!--<el-button type="danger" icon="el-icon-delete" circle @click="del(scope.row.id)"/>-->
      <!--</template>-->
      <!--</el-table-column>-->
    </el-table>
    <!--页码-->
    <div class="pagination-container">
      <el-pagination :current-page="listQuery.page" :page-sizes="[10,20,30, 50]" :page-size="listQuery.limit" :total="total" background layout="total, sizes, prev, pager, next, jumper" @size-change="handleSizeChange" @current-change="handleCurrentChange"/>
    </div>
  </div>
</template>

<script>
import { getList, del } from '@/api/opinion/opinion'
import store from '@/store'
import waves from '@/directive/waves' // 水波纹指令
import addPage from './addPage'
import editPage from './editPage'
import { parseTime } from '@/utils/index'
import viewContent from './viewContent'

export default {
  name: 'ComplexTable',
  directives: {
    waves
  },
  components: { addPage, editPage, viewContent },
  filters: {
    formatDate: function(time) {
      if (!time) {
        return ''
      }
      return parseTime(time)
    }
  },

  data() {
    return {
      tableKey: 0,
      list: null,
      total: null,
      listLoading: true,
      select_user_type: [
        {
          value: '选项1',
          label: '普通用户'
        }, {
          value: '选项2',
          label: '商家用户'
        }
      ],
      listQuery: {
        id: undefined,
        user_id: undefined,
        user_type: undefined,
        content: undefined,
        create_time: undefined,
        user_name: undefined,
        user_type_name: undefined,
        page: 1,
        limit: 20
      },
      formThead: {
        id: true,
        user_id: true,
        user_type: true,
        content: true,
        user_name: true,
        user_type_name: true,
        create_time: true
      }
    }
  },
  created() {
    this.getLists()
  },
  methods: {
    getLists() {
      this.listLoading = true
      getList(store.getters.token, this.listQuery).then(response => {
        for (let i = 0; i < Object.keys(response.data.list.data).length; i++) {
          response.data.list.data[i].create_time *= 1000
        }
        this.list = response.data.list.data
        this.total = response.data.list.total
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getLists()
    },
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.getLists()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getLists()
    },
    del(id) {
      this.$confirm('此操作将永久删除该意见反馈, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message.info('正在删除，ID：' + id)
        del(store.getters.token, { id: id }).then(res => {
          if (res.status) {
            this.$message.success('已经删除，ID：' + id)
            this.getLists()
          } else {
            this.$message.error('删除失败，ID：' + id)
          }
        })
      }).catch(() => {
        this.$message.info('已取消删除，ID：' + id)
      })
    }
  }
}
</script>
